package com.hcy.StudiosData;

import com.entity.Movie;
import com.entity.Studios;
import com.entity.User;
import com.hcy.MovieData.AddFrame;
import com.hcy.MovieData.MovieFrame;
import com.hcy.UpdateFrame;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * 按钮的监听
 */
public class ButtonStudios implements ActionListener{

    //父面板
    private StudiosFrame studiosFrame;
    private int pn;
    private int pages;

    private JTable jTable;
    private int index;
    private Studios pnStudios;

    public ButtonStudios(StudiosFrame studiosFrame) {

        this.studiosFrame = studiosFrame;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        //重新得到最新的当前页和页数
        this.pn = this.studiosFrame.getPn();
        this.pages = this.studiosFrame.getPages();

        //就是活动的这个组件
        JButton jbt = (JButton) e.getSource();
        switch (jbt.getText()) {
            case "增加":
                //创建一个更新数据的面板
                new StudiosAppFrame(this.studiosFrame, "增加数据", true);
                //刷新数据
                this.studiosFrame.flushStudios();
                //显示最后一行
                //设置当前页
                studiosFrame.setPn(this.studiosFrame.getPages());
                //显示这一页的数据
                studiosFrame.showStudios();
                break;
            case "删除":
                jTable = this.studiosFrame.getJt_showData();
                index = jTable.getSelectedRow();
                if (index == -1){
                    JOptionPane.showMessageDialog(this.studiosFrame, "请选择要删除的数据!!!");
                    return;
                }
                int select = JOptionPane.showConfirmDialog(this.studiosFrame, "确认删除吗？删除后数据不可恢复！！！");
                if (select != 0) {
                    return;
                }

                //获取当前页的集合，并获取这个用户对象
                pnStudios = this.studiosFrame.getPnList().get(index);
                //调用删除方法
                int i =  this.studiosFrame.getStudiosDao().deleteStudios(pnStudios.getSid());
                //弹出提示
                JOptionPane.showMessageDialog(this.studiosFrame, i>0?"删除成功":"删除失败");
                //更新当前页
                //刷新数据
                this.studiosFrame.flushStudios();
                //显示这一页的数据
                studiosFrame.showStudios();
                break;
            case "更新":
                jTable = this.studiosFrame.getJt_showData();
                index = jTable.getSelectedRow();
                if (index == -1){
                    JOptionPane.showMessageDialog(this.studiosFrame, "请选择要更新的数据!!!");
                    return;
                }
                //获取当前页的集合，并获取这个用户对象
                pnStudios = this.studiosFrame.getPnList().get(index);
                this.studiosFrame.setPnStudios(pnStudios);
                //弹出更新面板
                new StudiosAppFrame(this.studiosFrame, "修改数据", true);
                studiosFrame.setPnStudios(null);
                this.studiosFrame.flushStudios();
                this.studiosFrame.showStudios();

                break;
            case "刷新":
                this.studiosFrame.flushStudios();
                this.studiosFrame.showStudios();
                break;
            case "上一页":
                //设置当前页
                studiosFrame.setPn(pn <= 1?1:pn-1);
                //显示这一页的数据
                studiosFrame.showStudios();
                break;
            case "下一页":
                studiosFrame.setPn(pn >= pages?pages:pn+1);
                //显示这一页的数据
                studiosFrame.showStudios();
                break;
        }
    }
}